"""
Case Type   : 系统视图PG_PREPARED_STATEMENT
Case Name   : 开启线程池，不传入参数查询（使用旧视图）
Create At   : 2023/12/12
Owner       : ningyali
Description :
    1.开启线程池
    2.建表
    3.新建10个session，分别创建预备语句
    4.不传入参数查询视图
    5.清理环境
Expect      :
    1.执行成功
    2.执行成功
    3.执行成功
    4.只能查询到当前session的预备语句
    5.执行成功
History     :
"""

import os
import time
import unittest

from testcase.utils.ComThread import ComThread
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class PgPreparedStatement35(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        text = f'-----{os.path.basename(__file__)} start-----'
        self.log.info(text)
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.t_name = 't_prepare_35'
        self.prepare_name = 'stmt'
        self.prepare_sql = f"prepare {self.prepare_name} as " \
            f"insert into {self.t_name} values(1, 'openGauss');"
        self.query_sql = 'select * from pg_prepared_statement();'

    def test_1(self):
        text = 'step1:开启线程池; expect:执行成功'
        self.log.info(f'-----{text}-----')
        guc_res = self.pri_sh.execute_gsguc('set',
                                            self.constant.GSGUC_SUCCESS_MSG,
                                            'enable_thread_pool=on')
        self.assertTrue(guc_res, f'执行失败：{text}')
        restart_res = self.pri_sh.restart_db_cluster()
        self.assertTrue(restart_res, f'执行失败：{text}')

        text = 'step2:建表; expect:执行成功'
        self.log.info(f'-----{text}-----')
        sql = f'drop table if exists {self.t_name};' \
            f'create table {self.t_name}(c1 int, c2 text);'
        self.log.info(sql)
        res = self.pri_sh.execut_db_sql(sql)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, res, f'执行失败：{text}')

        text = 'step3:新建10个session，分别创建预备语句; expect:执行成功'
        self.log.info(f'-----{text}-----')
        threads = [ComThread(self.pri_sh.execut_db_sql,
                             args=(self.prepare_sql + 'select pg_sleep(5);',))
                   for i in range(10)]
        for t in threads:
            t.setDaemon(True)
            t.start()

        time.sleep(2)
        text = 'step4:不传入参数查询视图; expect:只能查询到当前session的预备语句'
        self.log.info(f'-----{text}-----')
        res = self.pri_sh.execut_db_sql(self.prepare_sql + self.query_sql)
        self.log.info(res)
        self.assertEqual(res.count(self.prepare_name), 2, f'执行失败：{text}')

        text = '获取线程结果'
        self.log.info(f'-----{text}-----')
        for t in threads:
            t.join(180)
            t_res = t.get_result()
            self.log.info(f'{t_res}')
            self.assertIn('PREPARE', t_res, f'执行失败：{text}')

    def tearDown(self):
        self.log.info('----run teardown----')
        text1 = 'step5:清理环境; expect:执行成功'
        self.log.info(f'-----{text1}-----')
        sql = f'drop table if exists {self.t_name} cascade;'
        res = self.pri_sh.execut_db_sql(sql)
        self.log.info(res)

        text2 = 'step6:关闭线程池; expect:执行成功'
        self.log.info(f'-----{text2}-----')
        guc_res = self.pri_sh.execute_gsguc('set',
                                            self.constant.GSGUC_SUCCESS_MSG,
                                            'enable_thread_pool=off')
        restart_res = self.pri_sh.restart_db_cluster()

        self.assertTrue(guc_res, f'执行失败：{text2}')
        self.assertTrue(restart_res, f'执行失败：{text2}')
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, res, f'执行失败：{text1}')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
